package com.shujia.flink.tf

import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector

object Demo2FlatMap {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val ds: DataStream[String] = env.socketTextStream("master", 8888)

    //scala的方式，函数返回一个数组
    val ds1: DataStream[String] = ds.flatMap(_.split(","))

    //java 的方式 使用匿名内部类
    val ds2: DataStream[String] = ds.flatMap(new FlatMapFunction[String, String] {
      override def flatMap(value: String, out: Collector[String]): Unit = {
        val array: Array[String] = value.split(",")
        for (elem <- array) {
          //发送数据到下游
          out.collect(elem)
        }
      }
    })


    //scala 升级版
    val ds3: DataStream[String] = ds.flatMap((value: String, out: Collector[String]) => {
      val array: Array[String] = value.split(",")
      for (elem <- array) {
        //将数据发送到下游
        out.collect(elem)
      }
    })

    ds3.print()


    env.execute()

  }
}
